package cn.iocoder.yudao.module.trade.service.storeusers;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.trade.controller.admin.storeusers.vo.StoreUsersPageReqVO;
import cn.iocoder.yudao.module.trade.controller.admin.storeusers.vo.StoreUsersSaveReqVO;
import cn.iocoder.yudao.module.trade.dal.dataobject.storeusers.StoreUsersDO;
import cn.iocoder.yudao.module.trade.dal.mysql.storeusers.StoreUsersMapper;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.trade.enums.ErrorCodeConstants.STORE_USERS_NOT_EXISTS;

/**
 * 店铺账号 Service 实现类
 *
 * @author 万民匠心
 */
@Service
@Validated
public class StoreUsersServiceImpl implements StoreUsersService {

    @Resource
    private StoreUsersMapper storeUsersMapper;
    @Resource
    private PasswordEncoder passwordEncoder;

    private static final String DEFAULT_PWD = "123456";

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long createStoreUsers(StoreUsersSaveReqVO createReqVO) {
        // 插入
        String username = "";
        StoreUsersDO storeUsers = BeanUtils.toBean(createReqVO, StoreUsersDO.class);
        storeUsers.setUsername(username);
        storeUsers.setStatus(Boolean.TRUE);
        storeUsers.setPassword(encodePassword(DEFAULT_PWD)); // 加密密码
        storeUsersMapper.insert(storeUsers);

        username = String.valueOf(10000 + storeUsers.getId());
        storeUsers.setUsername(username);
        storeUsersMapper.updateById(storeUsers);

        // 返回
        return storeUsers.getId();
    }

    @Override
    public void updateStoreUsers(StoreUsersSaveReqVO updateReqVO) {
        // 校验存在
        validateStoreUsersExists(updateReqVO.getId());
        // 更新
        StoreUsersDO updateObj = BeanUtils.toBean(updateReqVO, StoreUsersDO.class);
        storeUsersMapper.updateById(updateObj);
    }

    @Override
    public void deleteStoreUsers(Long id) {
        // 校验存在
        validateStoreUsersExists(id);
        // 删除
        storeUsersMapper.deleteById(id);
    }

    private void validateStoreUsersExists(Long id) {
        if (storeUsersMapper.selectById(id) == null) {
            throw exception(STORE_USERS_NOT_EXISTS);
        }
    }

    @Override
    public StoreUsersDO getStoreUsers(Long id) {
        return storeUsersMapper.selectById(id);
    }

    @Override
    public PageResult<StoreUsersDO> getStoreUsersPage(StoreUsersPageReqVO pageReqVO) {
        return storeUsersMapper.selectPage(pageReqVO);
    }

    @Override
    public void updateUserPassword(Long id) {
        StoreUsersDO storeUsers = storeUsersMapper.selectById(id);
        if (storeUsers == null) {
            throw exception(STORE_USERS_NOT_EXISTS);
        }
        storeUsers.setPassword(encodePassword(DEFAULT_PWD));
        storeUsersMapper.updateById(storeUsers);
    }

    @Override
    public void updateUserStatus(Long id, Boolean status) {
        StoreUsersDO storeUsers = storeUsersMapper.selectById(id);
        if (storeUsers == null) {
            throw exception(STORE_USERS_NOT_EXISTS);
        }
        storeUsers.setStatus(status);
        storeUsersMapper.updateById(storeUsers);
    }

    private String encodePassword(String password) {
        return passwordEncoder.encode(password);
    }

}